/**
  扩展一个 自定义 模块
**/

layui.define('jquery', function (exports) { //提示：模块也可以依赖其它模块，如：layui.define('mod1', callback);
    var $ = layui.$;
    var obj = {
        storeTableKey: 'FaceWebAPI',//存储数据操作的key值
        jwtTokenKey:'loginToken',
        SaveJWTToken: function (token) {//保存JWT Token值
            layui.data(this.storeTableKey, { key: this.jwtTokenKey, value: token });
        },
        GetJWTToken: function () {
            var mySetting = layui.data(this.storeTableKey);
            if (mySetting.hasOwnProperty(this.jwtTokenKey) == false) {
                return "";
            }
            return mySetting[this.jwtTokenKey];
        },
        CheckJWTToken: function (autoGoto,showbox) {
            var sToken = this.GetJWTToken();

            if (sToken == "") {
                if (autoGoto) this.GotoLogin(showbox);
                return "";
            }
            return sToken;
        },
        Ajax: function (par) {//
            var sToken = this.CheckJWTToken(true,true);
            if (sToken == "") return;

            var that = this;
            //自定义Ajax
            $.ajax({
                type: par.type,
                url: par.url,
                data: par.data,
                contentType: par.contentType,
                processData: par.processData,
                headers: {
                    //token: par.token,
                    Authorization: "Bearer " + sToken
                },
                success: function (result) {
                    if (result.result == false) {
                        switch (result.errCode) {
                            case 401:
                                that.GotoLogin(false);
                                return;
                            default:
                                if (par.hasOwnProperty("success")) {
                                    par.success(result);//交给上层处理
                                }
                        }
                    }
                    else {
                        if (par.hasOwnProperty("success")) {
                            par.success(result);//交给上层处理
                        }
                    }
                },
                complete: par.complete
            });
        },
        GotoLogin: function (showbox) {
            layui.data(this.storeTableKey, null); //删除test表
            if(showbox) {
                layer.alert('请重新登录！', { icon: 2 }, function () {
                    window.location = 'page/login.html';
                });
            }
            else {
                window.location = 'page/login.html';
            }
        }
    };

    //输出 mymod 接口
    exports('faceAPI', obj);
}); 